package 我的Java学习_算法基础.day_07;

/**
 * @author 挚爱之夕
 * @date 2021/8/20 - 08 - 20 - 9:04
 * @Description: 我的暑假Java学习.day_07
 * @Version: 1.0
 */
public class Test {
    public static void main(String[] args) {
        int[][] grid = new int[][]{
                {0, 0, 1, 0, 0},
                {0, 1, 0, 1, 0},
                {0, 0, 1, 0, 0}
        };
        System.out.println(new Test().maxAreaOfIsland(grid));
    }

    public int maxAreaOfIsland(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1) {
                    dfs(grid, i, j);
                    area = 0;
                }
            }
        }
        int res = max ;
        max = 0;
        return res;
    }
    static int area = 0;
    static int max = 0;
    private void dfs(int[][] grid, int i, int j) {
        area++;
        if(area>max)
            max = area;
        grid[i][j] = 0;
        for (int k = -1; k < 2; k++) {
            for (int l = -1; l < 2; l++) {
                if (l == 0 && k == 0)
                    continue;
                if (i + k >= 0 && i + k < grid.length && j + l >= 0 && j + l < grid[0].length) {
                    if (l == 0 || k == 0) { //四个方位：东西南北
                        if (grid[i + k][j + l] == 1)
                            dfs(grid, i + k, j + l);
                    }
                }
            }
        }
    }


}
